package com.lebang.configuration.interceptor;


import com.alibaba.fastjson.JSONObject;
import com.lebang.entity.LbbAppVersion;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.AppDictFeign;
import com.lebang.model.AppDictModel;
import com.lebang.model.RequestUserInfo;
import com.lebang.service.buss.AppVersionService;
import com.lebang.service.sys.DictService;
import com.lebang.service.sys.PkgBlackLogService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 自定义拦截器
 * 对指定接口进行封禁校验
 *
 * @author Eli
 */
@Component
public class BlackPkgInterceptor implements HandlerInterceptor {
    @Autowired
    private PkgBlackLogService pkgBlackLogService;
    @Autowired
    private DictService dictService;
    @Autowired
    private AppVersionService appVersionService;
    @Autowired
    private AppDictFeign feign;

    /**
     * 该方法将在请求处理之前进行调用，只有该方法返回true，才会继续执行后续的Interceptor和Controller，
     * 当返回值为true 时就会继续调用下一个Interceptor的preHandle 方法，
     * 如果已经是最后一个Interceptor的时候就会是调用当前请求的Controller方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        RequestUserInfo userInfo=new RequestUserInfo(request);
        if (!(object instanceof HandlerMethod)) {
            return true;
        }

        String version=requestHeaders.getVersion();
        String appStore=requestHeaders.getAppStore();
        String appId = requestHeaders.getAppid();
        Integer fail = pkgBlackLogService.pkgBlackLogService(requestHeaders);
        if (fail != null) {
            LbbAppVersion v2 = appVersionService.getByInnerCodeAndappStoreAndAppId(Integer.parseInt(version), appStore, appId);
            if(v2==null||v2.getOpenBlackPkg()==null||v2.getOpenBlackPkg()==1){
//                String qq = dictService.selectByKeyName("manager_qq").getKeyvalue();
                JsonData jsonData = feign.get("manager_qq", userInfo.getAppId());
                if (jsonData.isSuccess()){
                    AppDictModel appDictModel = JSONObject.parseObject(JSONObject.toJSONString(jsonData.getData()), AppDictModel.class);
                    responseMsg(response, "600", "账户异常禁止登陆,请联系管理员Q号:"+appDictModel.getKeyvalue()+"!");
                }
                return false;
            }
        }

        return true;
    }

    private void responseMsg(HttpServletResponse response, String status, String msg) throws IOException {
        JsonData jsonData = Callback.fail(status, msg, "");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; CHARSET=utf-8");
        String json = JSONObject.toJSONString(jsonData);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
        out.close();
    }


    /**
     * 该方法将在请求处理之后，DispatcherServlet进行视图返回渲染之前进行调用，可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行，该方法将在整个请求结束之后，也就是在DispatcherServlet 渲染了对应的视图之后执行。用于进行资源清理
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
